package com.jy.adscm.operation.lxcg.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.operation.lxcg.bean.LxcgBomSupplier;
import com.jy.adscm.operation.lxcg.bean.LxcgMaterial;
import com.jy.adscm.operation.lxcg.bean.QueryLxcgMaterial;
import com.jy.adscm.operation.lxcg.mapper.mysql.LxcgBomSupplierMapper;
import com.jy.adscm.operation.lxcg.mapper.mysql.LxcgMaterialMapper;
import com.jy.adscm.operation.lxcg.service.LxcgMaterialService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.apache.commons.collections.list.AbstractListDecorator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 零星基础物料表 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-08-15
 */
@Service
public class LxcgMaterialServiceImpl extends ServiceImpl<LxcgMaterialMapper, LxcgMaterial> implements LxcgMaterialService {
    @Autowired
    JyUserMapper jyUserMapper;
    @Autowired
    LxcgBomSupplierMapper lxcgBomSupplierMapper;
    @Override
    public Page<LxcgMaterial> selectLxcgMaterial(Page<LxcgMaterial> page, QueryLxcgMaterial queryLxcgMaterial) {

        List<LxcgMaterial> result=new ArrayList<>();
        long total = 0;
        if (!AdscmBaseUtils.getInstance().isNotEmpty(queryLxcgMaterial.getSupplier(),null)){
            Wrapper<LxcgMaterial> lxcgMaterialWrapper =new EntityWrapper<>();
            if (AdscmBaseUtils.getInstance().isNotEmpty(queryLxcgMaterial.getName(),null)){
                lxcgMaterialWrapper.like("name",queryLxcgMaterial.getName());
            }
            page= super.selectPage(page,lxcgMaterialWrapper);
            result = page.getRecords();
            total=super.selectCount(lxcgMaterialWrapper);
        }else {
            Wrapper<LxcgBomSupplier> lxcgBomSupplierWrapper=new EntityWrapper<>();
            lxcgBomSupplierWrapper.eq("sid",queryLxcgMaterial.getSupplier());
            List<LxcgBomSupplier> lxcgBomSuppliers = lxcgBomSupplierMapper.selectList(lxcgBomSupplierWrapper);
            for (LxcgBomSupplier lxcgBomSupplier : lxcgBomSuppliers) {
                LxcgMaterial lxcgMaterial = super.selectById(lxcgBomSupplier.getBid());
                result.add(lxcgMaterial);
            }
          total=result.size();
        }
        for (LxcgMaterial lxcgMaterial : result) {
            if (AdscmBaseUtils.getInstance().isNotEmpty( lxcgMaterial.getCreator(),null)){
                JyUser jyUser = jyUserMapper.selectById(lxcgMaterial.getCreator());
                lxcgMaterial.setCreatorName(jyUser.getName());
            }

        }

page.setRecords(result);
        page.setTotal(total);

        return page;
    }

    @Override
    public List<LxcgMaterial> selectByKeyWords(String name) {
        Wrapper<LxcgMaterial> wrapper=new EntityWrapper<>();
        wrapper.like("name",name);
        List<LxcgMaterial> lxcgMaterials = super.selectList(wrapper);
        return lxcgMaterials;
    }

    @Override
    public List<LxcgMaterial> selectBySid(Integer sid) {
        List<LxcgMaterial>  result =new ArrayList<>();
Wrapper<LxcgBomSupplier> lxcgBomSupplierWrapper=new EntityWrapper<>();
        lxcgBomSupplierWrapper.eq("sid",sid);
        List<LxcgBomSupplier> lxcgBomSuppliers = lxcgBomSupplierMapper.selectList(lxcgBomSupplierWrapper);
        for (LxcgBomSupplier lxcgBomSupplier : lxcgBomSuppliers) {
            LxcgMaterial lxcgMaterial = super.selectById(lxcgBomSupplier.getBid());
            lxcgMaterial.setLxcgBomSupplier(lxcgBomSupplier);
            result.add(lxcgMaterial);
        }
        return result;
    }
}
